package com.sakura.mall.member.memberLevel.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.memberLevel.domain.entity.MemberLevelEntity;
import com.sakura.mall.member.memberLevel.domain.request.AddMemberLevelRequest;
import com.sakura.mall.member.memberLevel.domain.request.UpdateMemberLevelRequest;
import com.sakura.mall.member.memberLevel.domain.request.MemberLevelPageRequest;
import com.sakura.mall.member.memberLevel.domain.vo.MemberLevelVo;
import com.sakura.mall.member.memberLevel.mapper.MemberLevelMapper;
import com.sakura.mall.member.memberLevel.service.MemberLevelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员等级 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevelEntity> implements MemberLevelService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberLevelRequest request) {
        // todo 其他校验逻辑
        MemberLevelEntity memberLevel = request.convert(MemberLevelEntity.class);
        this.save(memberLevel);
    }

    @Override
    public Page<MemberLevelVo> queryPage(MemberLevelPageRequest request) {
        IPage<MemberLevelEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberLevelEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberLevelVo> ret = qp.convert(entity -> entity.convert(MemberLevelVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberLevelRequest request) {
        MemberLevelEntity entity = request.convert(MemberLevelEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

